package repositories

import (
	"blog-backend/internal/app/models"
	"blog-backend/pkg/database"
)

type TagRepository struct{}

func NewTagRepository() *TagRepository {
	return &TagRepository{}
}

// Create 创建标签
func (r *TagRepository) Create(tag *models.Tag) error {
	return database.DB.Create(tag).Error
}

// GetByID 通过ID获取标签
func (r *TagRepository) GetByID(id uint) (*models.Tag, error) {
	var tag models.Tag
	result := database.DB.First(&tag, id)
	if result.Error != nil {
		return nil, result.Error
	}
	return &tag, nil
}

// GetByName 通过名称获取标签
func (r *TagRepository) GetByName(name string) (*models.Tag, error) {
	var tag models.Tag
	result := database.DB.Where("name = ?", name).First(&tag)
	if result.Error != nil {
		return nil, result.Error
	}
	return &tag, nil
}

// GetAll 获取所有标签
func (r *TagRepository) GetAll() ([]models.Tag, error) {
	var tags []models.Tag
	result := database.DB.Find(&tags)
	return tags, result.Error
}

// GetPopularTags 获取热门标签（按使用次数排序）
func (r *TagRepository) GetPopularTags(limit int) ([]models.Tag, error) {
	var tags []models.Tag
	result := database.DB.Model(&models.Tag{}).
		Select("tags.*, COUNT(post_tags.post_id) as post_count").
		Joins("LEFT JOIN post_tags ON post_tags.tag_id = tags.id").
		Group("tags.id").
		Order("post_count DESC").
		Limit(limit).
		Find(&tags)
	return tags, result.Error
}

// ClearPostTags 清除文章的所有标签
func (r *TagRepository) ClearPostTags(postID uint) error {
	return database.DB.Exec("DELETE FROM post_tags WHERE post_id = ?", postID).Error
}

// AddTagToPost 为文章添加标签
func (r *TagRepository) AddTagToPost(postID, tagID uint) error {
	return database.DB.Exec("INSERT INTO post_tags (post_id, tag_id) VALUES (?, ?)", postID, tagID).Error
}

// RemoveTagFromPost 从文章移除标签
func (r *TagRepository) RemoveTagFromPost(postID, tagID uint) error {
	return database.DB.Exec("DELETE FROM post_tags WHERE post_id = ? AND tag_id = ?", postID, tagID).Error
}

// GetTagsForPost 获取文章的标签
func (r *TagRepository) GetTagsForPost(postID uint) ([]models.Tag, error) {
	var tags []models.Tag
	result := database.DB.Joins("JOIN post_tags ON post_tags.tag_id = tags.id").
		Where("post_tags.post_id = ?", postID).
		Find(&tags)
	return tags, result.Error
}

// GetPostsByTag 获取拥有指定标签的文章
func (r *TagRepository) GetPostsByTag(tagID uint, page, limit int) ([]models.Post, int64, error) {
	var posts []models.Post
	var total int64

	offset := (page - 1) * limit

	// 获取文章总数
	if err := database.DB.Model(&models.Post{}).
		Joins("JOIN post_tags ON post_tags.post_id = posts.id").
		Where("post_tags.tag_id = ? AND posts.status = 1", tagID).
		Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 获取分页数据
	result := database.DB.Preload("User").Preload("Category").
		Joins("JOIN post_tags ON post_tags.post_id = posts.id").
		Where("post_tags.tag_id = ? AND posts.status = 1", tagID).
		Order("published_at DESC").
		Offset(offset).Limit(limit).
		Find(&posts)

	if result.Error != nil {
		return nil, 0, result.Error
	}

	return posts, total, nil
}